//
//  SHTabPageDemo2ViewController.swift
//  RxSwiftDemo2019_03_31
//
//  Created by 远平 on 2021/12/28.
//  Copyright © 2021 远平. All rights reserved.
//

import UIKit

class SHTabPageDemo2ViewController: UIViewController {

    private lazy var tabLayout: BNTabLayout = {
        let layout = BNTabLayout.defaultLayout()
        layout.tabWidth = 50.0
        layout.leftAlign = true
        layout.lrMargin = 0
        layout.tabMargin = 0
        layout.lineColor = .clear
        return layout
    }()
    
    lazy var tabView: BNTabView = {
        let tabView = BNTabView(frame: CGRect(x: 0, y: 0, width: 160, height: 30), with: self.tabLayout)
        tabView.backgroundColor = .white
        tabView.delegate = self as BNTabViewDelegate
        tabView.layer.borderWidth = 0
        tabView.titleItems = self.titleItems
        return tabView
    }()
    
    private let titleItems: [BNTabTitleItem] = {
        var items = [BNTabTitleItem]()
        let titles = ["自选", "市场", "资讯"]
        for title in titles {
            let item = BNTabTitleItem()
            item.titleColor = UIColor.qmui_color(withHexString: "#878D9A") ?? .red
            item.titleSelectedColor = UIColor.qmui_color(withHexString: "#1D252C") ?? .red
            item.titleFont = UIFont.systemFont(ofSize: 16)
            item.titleSelectedFont = UIFont.systemFont(ofSize: 16, weight: .semibold)
            item.title = title
            items.append(item)
        }
        return items
    }()
    
    
    private lazy var pageView: BNPageView = {
        let view = BNPageView()
        view.contentView.isScrollEnabled = true
        view.parentViewController = self
        view.backgroundColor = .clear
        view.contentView.backgroundColor = .clear
        view.delegate = self
        view.viewControllers = [SHOptional2ViewController(), SHMarket2ViewController(), SHNewsHome2ViewController()]
        return view
    }()
    
    private let kTabHeight: CGFloat = 48
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.titleView = self.tabView
                
        tabView.reloadData()
        
        view.addSubview(self.pageView)
        

        pageView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalToSuperview().offset(YXConstant.navBarHeight())
        }
    }

}

extension SHTabPageDemo2ViewController: BNTabViewDelegate {
    func contentScrollView(offsetAt index: Int) {
        let offset = CGPoint(x: CGFloat(index) * pageView.contentView.bounds.size.width, y: 0.0)
        pageView.contentView.setContentOffset(offset, animated: false)
    }
    
    func tabView(_ tabView: BNTabView, didSelectedItemAt index: Int, with scrolling: Bool) {
        
        
    }
    
}

extension SHTabPageDemo2ViewController: BNPageViewDelegate {
    
    func contentView(_ contentView: UIScrollView, didChanged offset: CGPoint) {
        self.tabView.contentScrollViewDidChanged(offset, width: pageView.contentView.bounds.size.width)
    }
}
